package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class OtherStudentDao implements BaseStudentDao {
    //定义一个数组容器作为整个系统的数据仓库
    private static ArrayList<Student> stus = new ArrayList<>();//加入static关键字修饰,使其可以被所有对象共享使用

    static {
        System.out.println("----------------Loding----------------");
        //load01();
        //load02();
        load03();
    }

    public boolean addStudent(Student stu) {
        stus.add(stu);
        //reSave01();
        reSave02();
        return true;
    }

    //库管类中查询所有学生方法
    public Student[] findAllStudent() {
        return stus.toArray(new Student[stus.size()]);
    }

    public void deleteStudentById(String delId) {
        int index = getIndex(delId);
        stus.remove(index);
        //reSave01();
        reSave02();
    }

    public int getIndex(String id) {
        //先假设要查找的学号在容器中不存在
        int index = -1;
        //遍历数组容器
        for (int i = 0; i < stus.size(); i++) {
            //拿到每一个学生对象
            Student stu = stus.get(i);
            //判断当前学生对象的学号与查找的学号是否匹配
            if (stu != null && stu.getId().equals(id)) {
                //找到了就修改标记让index记录当前学生对象的索引
                index = i;
            }
        }
        return index;
    }

    @Override
    public void updateStudent(String updateId, Student newStu) {
        //根据学号查找对应的学生对象在容器中的索引位置
        int index = getIndex(updateId);
        //拿新的学生对象覆盖掉该索引位置的老对象,从而实现修改效果
        stus.set(index, newStu);
        //reSave01();
        reSave02();
    }

    public void reSave01() {
        //创建字节输出流对象
        try {
            FileOutputStream fos = new FileOutputStream("itheima-edu-info-manager\\stu.txt");
            for (int i = 0; i < stus.size(); i++) {
                Student stu = stus.get(i);
                fos.write(stu.toString().getBytes());
                fos.write(System.lineSeparator().getBytes());
            }
            fos.close();
        } catch (Exception e) {
        }
    }

    public static void load01() {
        try {
            FileInputStream fis = new FileInputStream("itheima-edu-info-manager\\stu.txt");
            byte[] bys = new byte[1024*1024];
            int len = fis.read(bys);

            if(len != -1) {
                String s = new String(bys, 0, len);

                String[] stu = s.split(System.lineSeparator());
                for (String allStu : stu) {
                    String[] split = allStu.split(",");
                    Student d = new Student(split[0], split[1], split[2], split[3]);
                    stus.add(d);
                }
            }
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void load02(){
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("itheima-edu-info-manager\\stu.txt"));
            byte[] bys = new byte[1024*1024];
            int len;
            while((len = bis.read(bys))!=-1){
                String s = new String(bys,0,len);
                String[] split = s.split(System.lineSeparator());
                for (String stu : split) {
                    String[] allStu = stu.split(",");
                    Student d = new Student(allStu[0],allStu[1],allStu[2],allStu[3]);
                    stus.add(d);
                }
            }
            bis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void reSave02(){
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("itheima-edu-info-manager\\stu.txt"));
            oos.writeObject(stus);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void load03(){
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("itheima-edu-info-manager\\stu.txt"));
            stus = (ArrayList<Student>)ois.readObject();
            ois.close();
        } catch (Exception e) {

        }
    }
}
